import 'dart:math';

import 'package:flutter/services.dart';
import 'package:lib_bt/send/PackageInterface.dart';
import 'package:lib_util/ByteUtils.dart';
import 'package:lib_util/CrcUtils.dart';
import 'package:ileddemo/code/ble/protocol/base/RectConverter.dart';
import 'package:ileddemo/code/ble/protocol/base/SizeConverter.dart';
import 'package:ileddemo/code/ble/protocol/base/SourceType.dart';
import 'package:flutter/material.dart';
import 'dart:typed_data';
import 'package:json_annotation/json_annotation.dart';
import 'package:image/image.dart' as img;
import 'package:lib_util/FileUtil.dart';

import '../graffiti/GraffitiSourceBean.dart';
import '../ColorSourceInterface.dart';
import 'GifDecodeUtil.dart';

part 'GifSourceBean.g.dart';

@JsonSerializable()
class GifSourceBean extends ColorSourceInterface {
  //图片文件路径
  String imagePath = "";

  @JsonKey(ignore: true)
  List<GraffitiSourceBean> frameList = [];

  void addFrame(GraffitiSourceBean graffitiSourceBean) {
    frameList.add(graffitiSourceBean);
  }

  void setImagePath(String path) {
    imagePath = path;
    notifyListeners();
  }

  Future<Uint8List> getGifBytes(String path) async {
    Uint8List bytes;
    if (path.startsWith("assets")) {
      //内置文件
      var data = await rootBundle.load(path);
      bytes = data.buffer.asUint8List();
    } else {
      //本地文件
      bytes = FileUtil.readFile(path);
    }
    return bytes;
  }

  void getGraffitiSourceBeanList() {}

  GifSourceBean() {
    sourceType = SourceType.TYPE_GIF;
  }

  @override
  Future<List<int>> getSendData(bleBean) async {
    const List<int> byteList = [0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01];
    List<int> data = [];

    if (bleBean.isSupportGif) {
      var bytes = await getGifBytes(imagePath);
      for (var b in bytes) {
        data.add(b);
      }
    } else {
      if (frameList.isEmpty) {
        var byte = await getGifBytes(imagePath);
        var util = GifDecodeUtil(name, id, screenSize, byte);
        var gitBean = util.caretGifBean();

        if (gitBean != null) {
          frameList = gitBean.frameList;
        }
      }

      for (var g in frameList) {
        data.addAll(await g.getSendData(bleBean));
      }
      data.addAll(ByteUtils.shortToBytes(speed));
    }
    return data;
  }

  //节目属性数据
  @override
  List<int> getProgramParameters(bleBean) {
    List<int> data = [];
    //分区起点
    data.addAll([0, 0, 0, 0]);
    //分区大小
    data.addAll(ByteUtils.shortToBytes(screenSize.width.toInt()));
    data.addAll(ByteUtils.shortToBytes(screenSize.height.toInt()));
    //预留
    data.addAll([0, 0, 0]);
    //节目类型
    if (bleBean.isSupportGif) {
      data.add(6);
    } else {
      data.add(sourceType);
    }

    //总帧数
    data.addAll(ByteUtils.shortToBytes(frameList.length));
    //特效
    data.add(effects);
    //速度
    data.add(speed);
    //停留时间
    data.add(max(speed, 10));
    //边框
    data.add(frameType);
    //亮度
    data.add(brightness);
    //预留
    data.addAll([0, 0, 0]);
    return data;
  }

  factory GifSourceBean.fromJson(Map<String, dynamic> json) => _$GifSourceBeanFromJson(json);

  @override
  Map<String, dynamic> toJson() => _$GifSourceBeanToJson(this);
}
